package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbMenu;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.MD5Utils;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.LoginVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author 31803
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-06-19 18:43:08
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;

    @Override
    public R login(LoginVo loginVo) {


        //查看用户是否存在
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, loginVo.getUserName()));
        if(tbUser==null){
            return R.erorr().setMsg("用户不存在");
        }

        //使用密码加密
        String encrypt = MD5Utils.encrypt(loginVo.getUserPass());
        System.out.println("加密密码"+encrypt);
        if(!tbUser.getUserPass().equals(encrypt)){
            return R.erorr().setMsg("密码不正确");
        }

        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", tbUser.getUserId())
                .claim("userName", tbUser.getUserName())
                .compact();
        //存入缓存
        String key="token-"+tbUser.getUserId();
        redisTemplate.opsForValue().set(key,token,1, TimeUnit.HOURS);
        tbUser.setToken(token);

        return R.ok().setData(tbUser);
    }

    @Override
    public R menuTreeList() {
        //从token获取用户ID
        String token = request.getHeader("token");
        if(token==null){
            return R.erorr().setMsg("token不存在请登录");
        }
        Integer userId = TokenUtils.getUserId(token);
        //根据用户ID获取权限列表
       List<TbMenu> tbMenus= tbUserMapper.selectMenuListById(userId);
       //封装树形列表菜单
        List<Tree<String>> treeList = TreeUtil.build(tbMenus, "0", (menu, tree) -> {
            tree.setId(menu.getMenuId().toString());
            tree.setName(menu.getMenuName());
            tree.setParentId(menu.getMenuPid().toString());
            tree.putExtra("menuPath", menu.getMenuPath());
        });
        return R.ok().setData(treeList);
    }

    @Override
    public R ShopList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<TbUser> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //获取用户列表
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(TbUser::getCreateTime);
        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, wrapper);
        //返回用户数据
        return R.ok().setData(tbUserPage);
    }
}




